package com.qzdsoft.erpcloud.service.act.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.qzdsoft.erpcloud.domain.act.CenterMsg;
import com.qzdsoft.erpcloud.mapper.act.CenterMsgMapper;
import com.qzdsoft.erpcloud.service.act.CenterMsgService;
import com.qzdsoft.erpcloud.vo.query.act.CenterMsgInfo;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.act.ActCenterMsgStatusEnum;

/**
 * Created by Yang
 */
@Service
public class CenterMsgServiceImpl implements CenterMsgService {

    @Autowired
    private CenterMsgMapper actCenterMsgMapper;

    @Override
    public CenterMsg selectById(Integer id) {
        return actCenterMsgMapper.selectByPrimaryKey(id);
    }

    @Override
    public LayTableResponse<CenterMsgInfo> selectData(Integer companyId, String title, Integer type, Integer status, Date timeFrom, Date timeTo, Integer start, Integer pageSize) {
        PageHelper.startPage(start, pageSize);
        List<CenterMsgInfo> list = actCenterMsgMapper.selectData(companyId, title, type, status, timeFrom, timeTo, start, pageSize);
        return new LayTableResponse<CenterMsgInfo>(list);
    }

    @Override
    public Response<String> edit(CenterMsg msg) {
        if(null != msg.getId()){        //  编辑
            actCenterMsgMapper.updateByPrimaryKey(msg);
            return Response.success("编辑成功");
        } else {                        //  新增
            if(null == msg.getStatus()){
                msg.setStatus(ActCenterMsgStatusEnum.weidu.getKey());
            }
            if(null == msg.getType()){
                //msg.setStatus(ActCenterMsgTypeEnum.weidu.getKey());
            }
            msg.setCtime(new Date());
            actCenterMsgMapper.insert(msg);
            return Response.success("新增成功");
        }
    }

    @Override
    public Response<String> del(Integer id) {
        actCenterMsgMapper.deleteByPrimaryKey(id);
        return new Response<String>(ResultEnum.SUCCESS);
    }
}
